<template>
    <div class="list">
        <div class="table" v-if="active==1">
            <div class="header" style="background-color: white">
                <el-page-header @back="goBack" content="客户页面" style="position: relative;top: 10px;left: 5px;">
                </el-page-header>
            </div>
            <el-divider></el-divider>
            <el-form :model="params" style="width: 100%;">
                <el-container>
                    <!--内容-->
                    <el-form-item>
                        <el-input v-model="params.content" style="width: 200px;"></el-input>
                    </el-form-item>
                    <!--字段类型-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="params.field" :value="params.field" style="width: 200px;">
                            <el-option
                                    v-for="type in typeList"
                                    :label="type.value"
                                    :value="type.key"
                                    :key="type.key"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <!--是否指派-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="params.assign" :value="params.assign">
                            <el-option label="是否指派" value=""></el-option>
                            <el-option value="1" label="是"></el-option>
                            <el-option value="0" label="否"></el-option>
                        </el-select>
                    </el-form-item>
                    <!--客户类型-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="params.status" :value="params.status">
                            <el-option label="请选择客户类型" value=""></el-option>
                            <el-option value="1" label="潜在客户"></el-option>
                            <el-option value="2" label="目标客户"></el-option>
                            <el-option value="3" label="正式客户"></el-option>
                            <el-option value="4" label="有效客户"></el-option>
                            <el-option value="5" label="无效客户"></el-option>
                            <el-option value="6" label="重点跟进"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item style="margin-left: 10px">
                        <el-button type="primary" @click="handleSearch">检索</el-button>
                        <el-button @click="handleAdd">添加</el-button>
                    </el-form-item>
                </el-container>
            </el-form>

            <el-table :border="true" :data="pageResult.records">
                <el-table-column label="序号" type="index" width="80px"></el-table-column>
                <el-table-column label="客户(委托人)" prop="client"></el-table-column>
                <el-table-column label="联系电话" prop="phone"></el-table-column>
                <el-table-column label="地区" prop="pertainarea"></el-table-column>
                <el-table-column label="登记时间" prop="addtime"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown trigger="click" @command="handleCommandClick">
                            <el-button>
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'update')">修改
                                </el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'service')">添加服务记录
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>

            <el-footer class="case-list-footer">
                <el-pagination
                        style="margin: 10px auto"
                        background
                        layout="prev, pager, next"
                        :total="pageResult.total"
                        :page-size="pageResult.size"
                        class="pagination"
                        :current-page="pageResult.current"
                        @current-change="handleIndexChange"
                >
                </el-pagination>
            </el-footer>
        </div>

        <div style="width: 100%;" v-if="active==2">
            <client-add
                    :parentClient="client"
                    :status="status"
                    @back="handleBack"
                    @update="handleUpdateSubmit"
                    @add="handleAddSubmit">
            </client-add>
        </div>
    </div>
</template>

<script>
    //引入组件
    const ClientAdd = () => import('./ClientAdd.vue')
    //引入服务类
    import ClientService from '../../../model/gs/ClientService.js'

    const clientService = ClientService.getInstance()

    export default {
        data() {
            return {
                status: 'add',
                active: 1,
                params: {
                    status: '',
                    field: 'client',
                    caseAssign: ''
                },
                pageResult: {},
                //搜索类型
                typeList: [
                    {
                        key: 'client',
                        value: '客户搜索（委托人搜索）'
                    },
                    {
                        key: 'phone',
                        value: '电话搜索'
                    }
                ],
                client: {}
            }
        },
        methods: {
            getClientList() {
                clientService.getByPage(this.params).then(response => {
                    if (response.data.code === 0) {
                        this.pageResult = response.data.data
                    }
                })
            },
            handleSearch() {
                this.params.page = 1
                this.getClientList()
            },
            //处理角标变化的方法
            handleCommandClick(command) {
                switch (command.type) {
                    case 'service':
                        this.handleAddService(command.client)
                        break
                    case 'update':
                        this.handleUpdate(command.client)
                        break
                }
            },
            //查看详情的方法
            handleAddService(client) {
                this.$router.push({
                    name:'EditLog',
                    params:{
                        client
                    }
                })
            },
            //角标发生变化之前的操作
            beforeHandlerCommand(client, type) {
                return {
                    client, type
                }
            },
            handleIndexChange(index) {
                this.params.page = index
                this.getClientList()
            },
            handleUpdate(client) {
                //根据id查询数据
                clientService.getById(client.id).then(response => {
                    if (response.data.code === 0) {
                        this.client = response.data.data
                        this.status = 'update'
                        this.active = 2
                    }
                })
            },
            handleUpdateSubmit(client) {
                //校验
                if (!clientService.check(client)) {
                    return
                }
                //发送请求
                clientService.update(client).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success({
                            message: '修改成功',
                            duration: 1000
                        })
                        //刷新数据
                        this.handleSearch()
                        //重置client对象
                        this.handleBack()
                    }
                })
            },
            handleAddSubmit(client){
                //校验
                if (!clientService.check(client)) {
                    return
                }
                //发送请求
                clientService.save(client).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success({
                            message: '添加成功',
                            duration: 1000
                        })
                        //刷新数据
                        this.handleSearch()
                        //重置client对象
                        this.handleBack()
                    } else {
                        this.$message.error({
                            message:response.data.message,
                            duration:1000
                        })
                    }
                })
            },
            handleAdd() {
                this.status = 'add'
                this.active = 2
            },
            handleBack() {
                this.client = {}
                this.active = 1
                this.status = 'add'
            },
            goBack() {
                history.back()
            },
            //控制是所有客户还是全部客户
            setIsOwn() {
                if (this.$route.path == '/MyClient') {
                    this.params.isOwn = '1'
                } else {
                    delete this.params.isOwn
                }
            }
        },
        watch: {
            $route: {
                handler() {
                    this.setIsOwn()
                    this.getClientList()
                },
                deep: true
            }
        },
        created() {
            this.setIsOwn()
            this.getClientList()
        },
        components: {
            ClientAdd
        }
    }
</script>

<style scoped>
    .list {
        width: 100%;
    }

    .case-list-footer {
        width: 100%;
        height: 10%;
        position: relative;
    }

    .case-list-footer .pagination {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
    }

    .table {
        width: 100%;
    }
</style>
